import java.util.concurrent.TimeUnit;

/**
 * Create by chenhd on 2021/8/28.
 * 方法二：使用wait notify
 */
public class ConcurrentTask2 {
    public String str;

    public static void main(String[] args) throws InterruptedException {
        ConcurrentTask2 concurrentTask = new ConcurrentTask2();
        new Thread(concurrentTask::putStr).start();
        System.out.println(concurrentTask.getStr());
    }

    public synchronized void putStr() {
        try {
            TimeUnit.SECONDS.sleep(5);
            this.str = "hello concurrent.";
            this.notify();
        } catch (InterruptedException e) {
            //log error
            e.printStackTrace();
        }
    }

    public synchronized String getStr() throws InterruptedException {
        this.wait();
        return str;
    }
}
